"""
Case Type   : mot
Case Name   : 打开enable_mot_server参数，使用gs_backup备份、恢复
Create At   : 2025/4/7
Owner       : @li-xin12345
Description : (编译带mot，不编译ENABLE_X86_RDTSCP，对应转测包)
    1、执行备份
    2、修改enable_mot_server=on
    3、关闭数据库
    4、恢复
    5、启动数据库
    6、查询参数是否恢复为off
    7、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
    5、成功
    6、成功
    7、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

primary_db_sh = CommonSH('PrimaryDbUser')
cluster_node_count = primary_db_sh.get_node_num()


@unittest.skipIf(3 > cluster_node_count, '不满足一主两备环境跳过')
class MotTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.std1_db_sh = CommonSH('Standby1DbUser')
        self.std2_db_sh = CommonSH('Standby2DbUser')
        self.pri_node_sh = Node('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.backup_dir = 'f_guc_mot_0027'
        self.param_name = 'enable_mot_server'
        self.default_value = self.common.show_param(self.param_name)
        self.backup_path = os.path.join(os.path.dirname(
            macro.DB_INSTANCE_PATH), self.backup_dir)

    def test_mot(self):
        text = '-----step1:执行备份;expect:成功-----'
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_backup -t backup --backup-dir={self.backup_path} --all"
        res = self.common.get_sh_result(self.pri_node_sh, cmd)
        self.assertIn('Successfully backed up cluster files', res,
                      '执行失败：' + text)

        self.log.info('-----step2:修改enable_mot_server=on;expect:成功-----')
        res = primary_db_sh.execute_gsguc('set',
                                          self.constant.GSGUC_SUCCESS_MSG,
                                          f'{self.param_name}=on')
        self.log.info(res)
        self.assertTrue(res, '执行失败：' + text)
        res = primary_db_sh.restart_db_cluster()
        self.log.info(res)
        status = primary_db_sh.get_db_cluster_status()
        self.assertTrue("Normal" or "Degrade" in status)
        res = self.common.show_param(self.param_name)
        self.log.info(res)
        self.assertEqual('on', res, '执行失败：' + text)

        self.log.info('-----step3:关闭数据库;expect:成功-----')
        stop_res = primary_db_sh.stop_db_instance()
        self.assertIn(self.constant.GS_CTL_STOP_SUCCESS_MSG, stop_res,
                      '执行失败：' + text)

        self.log.info('-----step4:恢复;expect:成功-----')
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_backup -t restore --backup-dir={self.backup_path} --all"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.pri_node_sh, cmd)
        self.assertIn('Successfully restored cluster files', res,
                      '执行失败：' + text)

        self.log.info('-----step5:启动数据库;expect:成功-----')
        start_res = primary_db_sh.start_db_instance()
        self.assertIn(self.constant.RESTART_SUCCESS_MSG, start_res,
                      '执行失败：' + text)

        self.log.info('-----step6:查询参数是否恢复为off;expect:成功-----')
        res = self.common.show_param(self.param_name)
        self.log.info(res)
        self.assertEqual('off', res, '执行失败：' + text)

    def tearDown(self):
        text = '-----step7:清理环境; expect:成功-----'
        self.log.info(text)
        cmd = f'rm -rf {self.backup_path};' \
              f'if [ -d {self.backup_path} ]; ' \
              f'then echo "exists"; else echo "not exists"; fi'
        rm_res = self.common.get_sh_result(self.pri_node_sh, cmd)

        res = self.common.show_param(self.param_name)
        self.log.info(res)
        if res != self.default_value:
            restore_param_res = primary_db_sh.execute_gsguc(
                'set', self.constant.GSGUC_SUCCESS_MSG,
                f'{self.param_name}={self.default_value}')
            self.log.info(restore_param_res)
            self.assertTrue(restore_param_res, '执行失败：' + text)
            res = primary_db_sh.restart_db_cluster()
            self.log.info(res)
            status = primary_db_sh.get_db_cluster_status()
            self.assertTrue("Normal" or "Degrade" in status)
        self.assertEqual('not exists', rm_res, '文件删除失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')